import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

//Heart of the Inventory Setup -- this establishes the List to Save
public class InventoryList implements ActionListener, WindowListener{

	private JFrame listFrame = null;  //  @jve:decl-index=0:visual-constraint="124,38"
	private JPanel listContentPane = null;

	private JTextArea listTextArea = null;

	private JButton saveButton = null;
	private JButton clearButton = null;
	private JButton deleteEntryButton = null;

	private ArrayList<InventoryItem> inventory = new ArrayList<InventoryItem>();  //  @jve:decl-index=0:

	private int inventoryInt = 0;

	private String location, user;
	private JLabel titleLabel = null;
	private JLabel userLabel = null;
	
	//Constructor
	public InventoryList(VisualInventory vi, String loc, String use){
		
		location = loc;
		user = use;
//		System.out.println(location + " || " + loc+"  == loc" + " ++++++ " + user + " || " + use );
		buildListGUI();
	}

	private JFrame getListFrame() {
		if (listFrame == null) {
			listFrame = new JFrame();
			listFrame.setSize(new Dimension(781, 474));
			listFrame.setContentPane(getListContentPane());
			listFrame.addWindowListener(this);
		}
		return listFrame;
	}

	private JPanel getListContentPane() {
		if (listContentPane == null) {
			userLabel = new JLabel();
			userLabel.setBounds(new Rectangle(451, 19, 209, 38));
			userLabel.setText("User:");
			titleLabel = new JLabel();
			titleLabel.setBounds(new Rectangle(43, 14, 187, 39));
			titleLabel.setText("Title:");
			listContentPane = new JPanel();
			listContentPane.setLayout(null);
			listContentPane.add(getListTextArea(), null);
			listContentPane.add(getSaveButton(), null);
			listContentPane.add(getClearButton(), null);
			listContentPane.add(getDeleteEntryButton(), null);
			listContentPane.add(titleLabel, null);
			listContentPane.add(userLabel, null);
		}
		return listContentPane;
	}

	private JTextArea getListTextArea() {
		if (listTextArea == null) {
			listTextArea = new JTextArea();
			listTextArea.setBounds(new Rectangle(16, 66, 735, 295));
			listTextArea.setEditable(false);

		}
		return listTextArea;
	}

	private JButton getSaveButton() {
		if (saveButton == null) {
			saveButton = new JButton();
			saveButton.setBounds(new Rectangle(641, 385, 105, 31));
			saveButton.setText("SAVE");
			saveButton.addActionListener(this);
		}
		return saveButton;
	}

	private JButton getClearButton() {
		if (clearButton == null) {
			clearButton = new JButton();
			clearButton.setBounds(new Rectangle(318, 389, 105, 31));
			clearButton.setText("CLEAR ALL");
			clearButton.addActionListener(this);
		}
		return clearButton;
	}

	private JButton getDeleteEntryButton() {
		if (deleteEntryButton == null) {
			deleteEntryButton = new JButton();
			deleteEntryButton.setBounds(new Rectangle(30, 389, 121, 30));
			deleteEntryButton.setText("DELETE ENTRY");
			deleteEntryButton.addActionListener(this);
		}
		return deleteEntryButton;
	}

	private void buildListGUI(){
		this.getListFrame();

		listFrame.setVisible(true);
		listFrame.setTitle("Inventory List for: "+ location +" By: "+ user );
		titleLabel.setText("Location: "+ location);
		userLabel.setText("User: " + user);
	}

	public void addToList(InventoryItem device){
		inventoryInt++;
		device.setIndexInt(inventoryInt);
		inventory.add(device);
		repopTextArea();
//		System.out.println("#Winning");
	}

	public ArrayList<InventoryItem> getInventory() {
		return inventory;
	}

	public void setInventory(ArrayList<InventoryItem> inventory) {
		this.inventory = inventory;
		this.listTextArea.setText("");
	}

	public void repopTextArea(){
		listTextArea.setText("");
		for(int a =0; a < inventory.size(); ++a){

			inventory.get(a).setIndexInt(a+1);
			listTextArea.append( inventory.get(a).getIndexInt()+ "     ||     " + inventory.get(a).getItemSerial()+ "     ||     " +inventory.get(a).getItemCategory() + "     ||      " + inventory.get(a).getItemModel() +  "    ||    " +  inventory.get(a).getItemNotes() +  "     ||     " + inventory.get(a).getLocation() + "     ||     " + inventory.get(a).getUser());
			listTextArea.append("\n");
		}
	}

	//Kills Off Entire Inventory ArrayList and TextArea
	private void sippOnThis(){
		inventory.removeAll(inventory);
		listTextArea.setText("");
		inventoryInt = 0;

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource() == saveButton){
			for(int i = 0; i < inventory.size(); ++i){
//				System.out.println(inventory.get(i).getItemSerial() + " " + inventory.get(i).getItemCategory());
			}
			new ChrisPerez(inventory, location, user);
		} // Ends SaveButton Conditional

		//Allows for the Deletion of Certain Items in the ArrayList
		if(arg0.getSource() == deleteEntryButton){
			new Pronk(this,location, user, inventory);	
		}
		//Deletes the entire arrayList
		if(arg0.getSource() == clearButton){
			this.sippOnThis();
		}

	} // Ends ActionPerformed

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}





}
